/* 加密算法详见文档https://www.udesk.cn/doc/thirdparty/webim */
// 生成x位的字母数字混合随机数
function generateRandomString(length) {
  let result = '';
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

function sha1(s) {
  const data = new Uint8Array(encodeUTF8(s));
  var i, j, t; ``;
  const l = ((data.length + 8) >>> 6 << 4) + 16; var s = new Uint8Array(l << 2);
  s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer);
  for (t = new DataView(s.buffer), i = 0; i < l; i++)s[i] = t.getUint32(i << 2);
  s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8);
  s[l - 1] = data.length << 3;
  const w = []; const f = [
    function () { return m[1] & m[2] | ~m[1] & m[3]; },
    function () { return m[1] ^ m[2] ^ m[3]; },
    function () { return m[1] & m[2] | m[1] & m[3] | m[2] & m[3]; },
    function () { return m[1] ^ m[2] ^ m[3]; }
  ]; const rol = function (n, c) { return n << c | n >>> (32 - c); };
  const k = [1518500249, 1859775393, -1894007588, -899497514];
  var m = [1732584193, -271733879, null, null, -1009589776];
  m[2] = ~m[0], m[3] = ~m[1];
  for (i = 0; i < s.length; i += 16) {
    const o = m.slice(0);
    for (j = 0; j < 80; j++) {
      w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
      t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
      m[1] = rol(m[1], 30), m.pop(), m.unshift(t);
    }
    for (j = 0; j < 5; j++)m[j] = m[j] + o[j] | 0;
  };
  t = new DataView(new Uint32Array(m).buffer);
  for (var i = 0; i < 5; i++)m[i] = t.getUint32(i << 2);

  const hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function (e) {
    return (e < 16 ? '0' : '') + e.toString(16);
  }).join('');
  return hex.toUpperCase();
}

function encodeUTF8(s) {
  let i; const r = []; let c; let x;
  for (i = 0; i < s.length; i++) {
    if ((c = s.charCodeAt(i)) < 0x80) r.push(c);
    else if (c < 0x800) r.push(0xC0 + (c >> 6 & 0x1F), 0x80 + (c & 0x3F));
    else {
      if ((x = c ^ 0xD800) >> 10 === 0) {
        c = (x << 10) + (s.charCodeAt(++i) ^ 0xDC00) + 0x10000,
        r.push(0xF0 + (c >> 18 & 0x7), 0x80 + (c >> 12 & 0x3F));
      } else r.push(0xE0 + (c >> 12 & 0xF));
      r.push(0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
    }
  };
  return r;
};

export function udeskInit(config = {}) {
  if (window.ud) return;
  const randomNum = generateRandomString(16);
  const nowTimeStr = Date.now().toString();
  const userInfo = localStorage.getItem('userInfo') || '';
  const cPhone = userInfo ? JSON.parse(userInfo)?.mobile : '';
  const cName = userInfo ? JSON.parse(userInfo)?.username : '';
  const cEmail = userInfo ? JSON.parse(userInfo)?.email : '';
  const imUserKey = 'd7f63a098fcee3937b1534fa179861da';
  const strSign = `nonce=${randomNum}&timestamp=${nowTimeStr}&web_token=${cPhone}&${imUserKey}`;
  const strSignature = sha1(strSign);
  (function (a, h, c, b, f, g) { a['UdeskApiObject'] = f; a[f] = a[f] || function () { (a[f].d = a[f].d || []).push(arguments); }; g = h.createElement(c); g.async = 1; g.charset = 'utf-8'; g.src = b; c = h.getElementsByTagName(c)[0]; c.parentNode.insertBefore(g, c); })(window, document, 'script', 'https://assets-cli.s2.udesk.cn/im_client/js/udeskApi.js', 'ud');
  ud({
    'code': '24581k88',
    'link': 'https://gitcode.s2.udesk.cn/im_client/?web_plugin_id=57707',
    'targetSelector': '#gitcode-desk-btn',
    'selector': '#gstar-desk-btn',
    'customer': {
      'nonce': randomNum,
      'timestamp': nowTimeStr,
      'web_token': cPhone,
      'c_name': cName,
      'c_phone': cPhone,
      'c_email': cEmail,
      'signature': strSignature
    },
    ...config
  });
};
